package com.lyj.io.module.sys.controller;

import com.lyj.io.annotation.SysLogAnnotation;
import com.lyj.io.common.utils.PageUtils;
import com.lyj.io.common.utils.R;
import com.lyj.io.common.dto.IdsDto;
import com.lyj.io.module.sys.entity.SysRole;
import com.lyj.io.module.sys.service.SysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;

/**
 * 角色相关api
 * @author: 李亚杰
 * @version: 1.0
 * @date: 2021/10/8 14:56
 */
@Api(value = "SysRoleController", tags = "角色相关api")
@RestController
@RequestMapping("/api/v1/role")
public class SysRoleController {
    @Autowired
    SysRoleService sysRoleService;

    /**
     * 查询角色列表
     * @param params
     * @return com.ancient.common.utils.R
     * @author: liyajie
     * @date: 2021/10/8 15:52
     * @exception:
     * @update:
     * @updatePerson:
     **/
    @SysLogAnnotation("查询角色列表")
    @ApiOperation(value = "查询角色列表", notes = "查询角色列表")
    @PostMapping("/getRoleList")
    public R getRoleList(@RequestBody Map<String, Object> params) {
        PageUtils page = sysRoleService.queryPage(params);
        return R.ok().put("page", page);
    }

    /**
     * 保存角色
     * @param sysRole
     * @return com.ancient.common.utils.R
     * @author: liyajie
     * @date: 2021/10/8 15:52
     * @exception:
     * @update:
     * @updatePerson:
     **/
    @SysLogAnnotation("保存角色")
    @ApiOperation(value = "保存角色", notes = "保存角色")
    @PostMapping("/addRole")
    public R save(@RequestBody SysRole sysRole) {
        sysRole.setCreateDate(new Date());
        sysRoleService.save(sysRole);
        return R.ok();
    }

    /**
     * 修改角色
     * @param sysRole
     * @return com.ancient.common.utils.R
     * @author: liyajie
     * @date: 2021/10/8 15:55
     * @exception:
     * @update:
     * @updatePerson:
     **/
    @SysLogAnnotation("修改角色")
    @ApiOperation(value = "修改角色", notes = "修改角色")
    @PostMapping("/updateRole")
    public R update(@RequestBody SysRole sysRole) {
        sysRoleService.updateById(sysRole);
        return R.ok();
    }

    /**
     * 删除角色
     * @param idsDto
     * @return com.ancient.common.utils.R
     * @author: liyajie
     * @date: 2021/10/8 15:56
     * @exception:
     * @update:
     * @updatePerson:
     **/
    @SysLogAnnotation("删除角色")
    @ApiOperation(value = "删除角色", notes = "删除角色")
    @PostMapping("/deleteRole")
    public R delete(@RequestBody IdsDto idsDto) {
        String ids = idsDto.getIds();
        sysRoleService.removeByIds(Arrays.asList(ids.split(",")));
        return R.ok();
    }
}
